/**
 * Created by alice on 2017/5/02 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var nowPage=1;
    var total;
    var ready = {
        layer: null,
        page_size: 5
    };
    var temp = new Date();
    var defaultStartTime = temp.getFullYear()+"-01-01";
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $("#endTime").find("input").val(utils.get_date(0));
    $("#startTime").find("input").val(defaultStartTime);
    var start = $("#startTime").find("input").val();
    var end = $("#endTime").find("input").val();
    var type=$("#type").val();
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();
        Class.count_major();
        function init_event() {//TODO 初始化页面事件
            $('#major_link').attr('href', r.base_path + 'views/major/major.html?court=' + r.court_id);
            initTable();
        }
        
        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
        }
    };
    function initTable(){
    	var table = $('#table');
        table.bootstrapTable({
            columns: [{
                field: 'Number',
                title: '序号',
                width: '5%',
                class: 'cmp-table-right',
                formatter: function (value, row, index) {
                    return (index + 1) > 9 ? index + 1 : '0' + (index + 1);
                }
            }, {
                field: 'casename',
                title: '案号',
                width: '10%'
            }, {
                field: 'name',
                title: '名称',
                width: '10%',
                formatter: function (result,row) {
                	var date=new Date(row.create_time);
                	var type=row.type;
                	var url="http://"+location.hostname+":8080/";
                	if(type==1){
                		//图片
                		 url=url+"images/";
                	}else{
                		//视频
                		 url=url+"videos/";
                	}
                	url=url+date.getFullYear()+"/"+(date.getMonth() + 1)+"/"+result;
                	//拼接下载地址
                	var a="<a  download  class='btn btn-primary' href='"+url+"'>"+result+"</a>";
                    return a;
                }
            }, {
                field: 'channelsame',
                title: '通道名称',
                width: '9%'
            }, {
                field: 'create_user',
                title: '创建人',
                width: '9%'
            }, {
                field: 'type',
                title: '类型',
                width: '9%',
                formatter: function (result) {
                	if(result==1){
                		return '图片';
                	}else{
                		return '视频';
                	} 
                }
            }, {
                field: 'create_time',
                title: '创建时间',
                width: '12%',
                formatter: function (result) {
                	var date = new Date(result);
                    var seperator1 = "-";
                    var seperator2 = ":";
                    var month = date.getMonth() + 1;
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    var strDate = date.getDate();
                    if (strDate >= 1 && strDate <= 9) {
                    	strDate = "0" + strDate;
                    }
                    var hours=date.getHours();
                    //时
                    if (hours >= 0 && hours <= 9) {
                    	hours = "0" + hours;
                    }
                    var minute=date.getMinutes();
                    //分
                    if (minute >= 0 && minute <= 9) {
                    	minute = "0" + minute;
                    }
                    //秒
                    var second=date.getSeconds();
                    //分
                    if (second >= 0 && second <= 9) {
                    	second = "0" + second;
                    }
                    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                            + " " + hours + seperator2 + minute + seperator2 + second;
                    return currentdate;
                }
            }, {
                field: 'start_time',
                title: '截取起点',
                width: '12%',
            },{
                field: 'end_time',
                title: '截取终点',
                width: '12%',
            },{
                field: '',
                title: '删除',
                width: '10%',
                formatter: function (result,row) {
                	var b="<button class='btn btn-primary' onclick='del(this);' id='"+row.id+"'>删除</button>";
                    return  b;
                }
            }],
            data: []
        });
    	
    }
    //TODO 内部调用的方法写这里
    var Class = {
        count_major: function () {
            $.get(r.base_path + "major/findSumImagesNum?start="+start+"&end="+end+"&type="+type, {}, function (result) {
                var s = result.data;
                total=s;
                if (s == 0) {
                    $("#table").html('<span class="context-white context-span" id="description">暂无截图/视频</span>');
                    Class.page(1, 1, null);
                    $('#loading').hide();
                } else {
                    Class.page(1, Math.ceil(s / ready.page_size), Class.select_major);
                    Class.select_major(1);
                }
            },'json');
        },
        select_major:function (page_no) {
        	nowPage=page_no;
            $.get(r.base_path + 'major/findImages', {
                page: page_no,
                pageSize: ready.page_size,
                start:start,
                end:end,
                type:type
            }, function (result) {
                var table = $('#table');
                if (result.status == 0) {
                    table.show();
                    if (result.data.length == 0) {
                    } else {
                        table.bootstrapTable("load", result.data);
                    }
                    // table.hide();
                }
                $('#loading').hide();
            },'json').fail(function () {
                $('#loading').hide();
            });
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {};
    window.del=function(e){
    	var id=$(e).attr("id");
    	$('#loading').show();
    	$.ajax({
    		"type":"post",
    		"url" :r.base_path+'del?'+new Date().getTime(),
    		"dataType":"json",
    		"data": {
                id:id
            },
            "success" : function(data) {
            	 //重新获取总页数
                     $.get(r.base_path + "major/findSumImagesNum?start="+start+"&end="+end+"&type="+type, {}, function (result) {
                         var s = result.data;
                         total=s;
                         if (s == 0) {
                             $("#table").html('<span class="context-white context-span" id="description">暂无截图/视频</span>');
                             Class.page(1, 1, null);
                             $('#loading').hide();
                         }else{
                             if(s%ready.page_size==0){
                        		 if(nowPage!=1){
                        			 nowPage=nowPage-1;
                        		 }
                        		 
                        	 } 
                           //重新加载数据源 
                         	 Class.select_major(nowPage);
                         	 //重新加载分页按钮
                         	Class.page(nowPage,Math.ceil(s / ready.page_size),null);
                         }  
                     },'json');
    		},
    		"error" : function(data) {
    			$('#loading').hide();
    			alert(data.msg);
    		}
    	}); 
    }
    //检索
    window.query=function(){
    	//进行业务判断
        start = $("#startTime").find("input").val();
        end = $("#endTime").find("input").val();
        type=$("#type").val();
        if(!start){
        	alert("开始日期不能为空"); 
        	
        	return;
        }
        if(!end){
        	alert("结束日期不能为空");
        	return;
        }
      //打开遮层
    	$("#loading").show();
    	$("#tt").html('');
    	$("#tt").html('<table id="table" hidden class="table-major context-white cursor-hand context-span"></table>');
    	initTable();
    	//查询数据
    	Class.count_major(); 
    }
   
}(window);

